/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lesson3.com.demo.lang.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import lesson3.com.demo.lang.MySecurityManager;


public class ReflectionExample {
    public static void main(String[] args) throws Exception{
        
        MySecurityManager sm = new MySecurityManager();
        
        Class cls = sm.getClass();
        
        cls = Class.forName("javax.swing.JButton");
        
        Method[] meths = cls.getMethods();
        Class[] params; int mods;
        for(Method m: meths){
            
            params = m.getParameterTypes();
            
            if(params.length!=0){
                continue;
            }
            if(m.getReturnType()==Void.TYPE){
                continue;
            }
            
            if(!(m.getName().startsWith("get") || m.getName().startsWith("is"))){
                continue;
            }
            
            mods = m.getModifiers();
            if(Modifier.isStatic(mods)){
                continue;
            }
            
            if(!(Modifier.isPublic(mods))){
                continue;
            }
            
            System.out.println(m.getName());
            
           // System.out.println("The parameters for this method are:");
           // for(Class p: params){
           //     System.out.println("\t"+p.getName());
           // }
            System.out.println("----");
        }
        
        Constructor con = cls.getConstructor(new Class[]{String.class});
        Object theButton = con.newInstance(new Object[]{"I am a JButton"});
        System.out.println("The JButton's toString() "+theButton);
        System.out.println("The JButton's text "+ ((javax.swing.JButton)theButton).getText());
        
        cls = lesson3.com.demo.ser.better.Cat.class;
        Field  versionUIDField = cls.getField("serialVersionUID");
        System.out.println("uid for Cat class is "+versionUIDField.get(null));
    }
}
